<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>eventhandler_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_eventhandler_test.js.html">eventhandler_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.events.EventHandlerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.events.EventHandlerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.events&#39;);
<a name="line19"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line20"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line23"></a>
<a name="line24"></a>var a, b, c, d, eh;
<a name="line25"></a>
<a name="line26"></a>function setUpPage() {
<a name="line27"></a>  a = document.getElementById(&#39;a&#39;);
<a name="line28"></a>  b = document.getElementById(&#39;b&#39;);
<a name="line29"></a>  c = document.getElementById(&#39;c&#39;);
<a name="line30"></a>  d = document.getElementById(&#39;d&#39;);
<a name="line31"></a>}
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>function tearDown() {
<a name="line35"></a>  goog.dispose(eh);
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function testEventHandlerClearsListeners() {
<a name="line39"></a>  function tmp() {}
<a name="line40"></a>
<a name="line41"></a>  goog.events.listen(a, &#39;click&#39;, tmp);
<a name="line42"></a>
<a name="line43"></a>  assertEquals(1, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line44"></a>
<a name="line45"></a>  eh = new goog.events.EventHandler();
<a name="line46"></a>  eh.listen(a, &#39;click&#39;);
<a name="line47"></a>  eh.listen(a, &#39;keypress&#39;);
<a name="line48"></a>  eh.listen(b, &#39;mouseover&#39;);
<a name="line49"></a>  eh.listen(c, &#39;mousedown&#39;);
<a name="line50"></a>  eh.listen(d, &#39;click&#39;);
<a name="line51"></a>  eh.listen(d, &#39;mousedown&#39;);
<a name="line52"></a>
<a name="line53"></a>  assertEquals(2, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line54"></a>  assertEquals(1, goog.events.getListeners(a, &#39;keypress&#39;, false).length);
<a name="line55"></a>  assertEquals(1, goog.events.getListeners(b, &#39;mouseover&#39;, false).length);
<a name="line56"></a>  assertEquals(1, goog.events.getListeners(c, &#39;mousedown&#39;, false).length);
<a name="line57"></a>  assertEquals(1, goog.events.getListeners(d, &#39;click&#39;, false).length);
<a name="line58"></a>  assertEquals(1, goog.events.getListeners(d, &#39;mousedown&#39;, false).length);
<a name="line59"></a>
<a name="line60"></a>  eh.unlisten(d, &#39;mousedown&#39;);
<a name="line61"></a>
<a name="line62"></a>  assertEquals(2, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line63"></a>  assertEquals(1, goog.events.getListeners(a, &#39;keypress&#39;, false).length);
<a name="line64"></a>  assertEquals(1, goog.events.getListeners(b, &#39;mouseover&#39;, false).length);
<a name="line65"></a>  assertEquals(1, goog.events.getListeners(c, &#39;mousedown&#39;, false).length);
<a name="line66"></a>  assertEquals(1, goog.events.getListeners(d, &#39;click&#39;, false).length);
<a name="line67"></a>  assertEquals(0, goog.events.getListeners(d, &#39;mousedown&#39;, false).length);
<a name="line68"></a>
<a name="line69"></a>  eh.dispose();
<a name="line70"></a>
<a name="line71"></a>  assertEquals(1, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line72"></a>  assertEquals(0, goog.events.getListeners(a, &#39;keypress&#39;, false).length);
<a name="line73"></a>  assertEquals(0, goog.events.getListeners(b, &#39;mouseover&#39;, false).length);
<a name="line74"></a>  assertEquals(0, goog.events.getListeners(c, &#39;mousedown&#39;, false).length);
<a name="line75"></a>  assertEquals(0, goog.events.getListeners(d, &#39;click&#39;, false).length);
<a name="line76"></a>  assertEquals(0, goog.events.getListeners(d, &#39;mousedown&#39;, false).length);
<a name="line77"></a>
<a name="line78"></a>  goog.events.unlisten(a, &#39;click&#39;, tmp);
<a name="line79"></a>  assertEquals(0, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testListenArray() {
<a name="line83"></a>  eh = new goog.events.EventHandler();
<a name="line84"></a>
<a name="line85"></a>  eh.listen(a, [&#39;click&#39;, &#39;mousedown&#39;, &#39;mouseup&#39;]);
<a name="line86"></a>
<a name="line87"></a>  assertEquals(1, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line88"></a>  assertEquals(1, goog.events.getListeners(a, &#39;mousedown&#39;, false).length);
<a name="line89"></a>  assertEquals(1, goog.events.getListeners(a, &#39;mouseup&#39;, false).length);
<a name="line90"></a>
<a name="line91"></a>  eh.unlisten(a, [&#39;click&#39;, &#39;mousedown&#39;, &#39;mouseup&#39;]);
<a name="line92"></a>
<a name="line93"></a>  assertEquals(0, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line94"></a>  assertEquals(0, goog.events.getListeners(a, &#39;mousedown&#39;, false).length);
<a name="line95"></a>  assertEquals(0, goog.events.getListeners(a, &#39;mouseup&#39;, false).length);
<a name="line96"></a>
<a name="line97"></a>  eh.listen(a, [&#39;click&#39;, &#39;mousedown&#39;, &#39;mouseup&#39;]);
<a name="line98"></a>
<a name="line99"></a>  assertEquals(1, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line100"></a>  assertEquals(1, goog.events.getListeners(a, &#39;mousedown&#39;, false).length);
<a name="line101"></a>  assertEquals(1, goog.events.getListeners(a, &#39;mouseup&#39;, false).length);
<a name="line102"></a>
<a name="line103"></a>  eh.removeAll();
<a name="line104"></a>
<a name="line105"></a>  assertEquals(0, goog.events.getListeners(a, &#39;click&#39;, false).length);
<a name="line106"></a>  assertEquals(0, goog.events.getListeners(a, &#39;mousedown&#39;, false).length);
<a name="line107"></a>  assertEquals(0, goog.events.getListeners(a, &#39;mouseup&#39;, false).length);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testListenOnceRemovesListenerWhenFired() {
<a name="line111"></a>  var target = new goog.events.EventTarget();
<a name="line112"></a>  eh = new goog.events.EventHandler();
<a name="line113"></a>  var handler = goog.testing.recordFunction();
<a name="line114"></a>  eh.listenOnce(target, &#39;click&#39;, handler);
<a name="line115"></a>
<a name="line116"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line117"></a>  assertEquals(&#39;One event should have been dispatched&#39;,
<a name="line118"></a>      1, handler.getCallCount());
<a name="line119"></a>
<a name="line120"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line121"></a>  assertEquals(&#39;No event should have been dispatched&#39;,
<a name="line122"></a>      1, handler.getCallCount());
<a name="line123"></a>}
<a name="line124"></a>
<a name="line125"></a>function testListenOnceListenerIsCleanedUp() {
<a name="line126"></a>  var target = new goog.events.EventTarget();
<a name="line127"></a>  eh = new goog.events.EventHandler();
<a name="line128"></a>  var handler = goog.testing.recordFunction();
<a name="line129"></a>  eh.listenOnce(target, &#39;click&#39;, handler);
<a name="line130"></a>
<a name="line131"></a>  eh.removeAll();
<a name="line132"></a>
<a name="line133"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line134"></a>  assertEquals(0, handler.getCallCount());
<a name="line135"></a>}
<a name="line136"></a>
<a name="line137"></a>function testClearListenersWithListenOnceListenerRemoved() {
<a name="line138"></a>  var target = new goog.events.EventTarget();
<a name="line139"></a>  eh = new goog.events.EventHandler();
<a name="line140"></a>
<a name="line141"></a>  var handler = goog.testing.recordFunction();
<a name="line142"></a>  eh.listenOnce(target, &#39;click&#39;, handler);
<a name="line143"></a>
<a name="line144"></a>  assertNotNull(goog.events.getListener(target, &#39;click&#39;, handler, false, eh));
<a name="line145"></a>
<a name="line146"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line147"></a>  assertEquals(&#39;One event should have been dispatched&#39;,
<a name="line148"></a>      1, handler.getCallCount());
<a name="line149"></a>
<a name="line150"></a>  assertNull(goog.events.getListener(target, &#39;click&#39;, handler, false, eh));
<a name="line151"></a>
<a name="line152"></a>  eh.removeAll();
<a name="line153"></a>
<a name="line154"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line155"></a>  assertEquals(&#39;No event should have been dispatched&#39;,
<a name="line156"></a>      1, handler.getCallCount());
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>function testListenOnceArray() {
<a name="line160"></a>  var target = new goog.events.EventTarget();
<a name="line161"></a>
<a name="line162"></a>  eh = new goog.events.EventHandler();
<a name="line163"></a>  var handler = goog.testing.recordFunction();
<a name="line164"></a>  eh.listenOnce(target, [&#39;click&#39;, &#39;mousedown&#39;, &#39;mouseup&#39;], handler);
<a name="line165"></a>
<a name="line166"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line167"></a>  assertEquals(&#39;1 event should have been dispatched&#39;,
<a name="line168"></a>      1, handler.getCallCount());
<a name="line169"></a>  assertEquals(&#39;Should be a click event&#39;,
<a name="line170"></a>      &#39;click&#39;, handler.getLastCall().getArgument(0).type);
<a name="line171"></a>
<a name="line172"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line173"></a>  assertEquals(&#39;No event should be dispatched&#39;,
<a name="line174"></a>      1, handler.getCallCount());
<a name="line175"></a>
<a name="line176"></a>  target.dispatchEvent(&#39;mouseup&#39;);
<a name="line177"></a>  assertEquals(&#39;1 event should have been dispatched&#39;,
<a name="line178"></a>      2, handler.getCallCount());
<a name="line179"></a>  assertEquals(&#39;Should be a mouseup event&#39;,
<a name="line180"></a>      &#39;mouseup&#39;, handler.getLastCall().getArgument(0).type);
<a name="line181"></a>
<a name="line182"></a>  target.dispatchEvent(&#39;mouseup&#39;);
<a name="line183"></a>  assertEquals(&#39;No event should be dispatched&#39;,
<a name="line184"></a>      2, handler.getCallCount());
<a name="line185"></a>
<a name="line186"></a>  target.dispatchEvent(&#39;mousedown&#39;);
<a name="line187"></a>  assertEquals(&#39;1 event should have been dispatched&#39;,
<a name="line188"></a>      3, handler.getCallCount());
<a name="line189"></a>  assertEquals(&#39;Should be a mousedown event&#39;,
<a name="line190"></a>      &#39;mousedown&#39;, handler.getLastCall().getArgument(0).type);
<a name="line191"></a>
<a name="line192"></a>  target.dispatchEvent(&#39;mousedown&#39;);
<a name="line193"></a>  assertEquals(&#39;No event should be dispatched&#39;,
<a name="line194"></a>      3, handler.getCallCount());
<a name="line195"></a>}
<a name="line196"></a>
<a name="line197"></a>function testListenUnlistenWithObjectHandler() {
<a name="line198"></a>  var target = new goog.events.EventTarget();
<a name="line199"></a>  eh = new goog.events.EventHandler();
<a name="line200"></a>  var handlerObj = {
<a name="line201"></a>    handleEvent: goog.testing.recordFunction()
<a name="line202"></a>  };
<a name="line203"></a>  eh.listen(target, &#39;click&#39;, handlerObj);
<a name="line204"></a>
<a name="line205"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line206"></a>  assertEquals(&#39;One event should have been dispatched&#39;,
<a name="line207"></a>      1, handlerObj.handleEvent.getCallCount());
<a name="line208"></a>
<a name="line209"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line210"></a>  assertEquals(&#39;One event should have been dispatched&#39;,
<a name="line211"></a>      2, handlerObj.handleEvent.getCallCount());
<a name="line212"></a>
<a name="line213"></a>  eh.unlisten(target, &#39;click&#39;, handlerObj);
<a name="line214"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line215"></a>  assertEquals(&#39;No event should have been dispatched&#39;,
<a name="line216"></a>      2, handlerObj.handleEvent.getCallCount());
<a name="line217"></a>}
<a name="line218"></a>
<a name="line219"></a>function testListenOnceWithObjectHandler() {
<a name="line220"></a>  var target = new goog.events.EventTarget();
<a name="line221"></a>  eh = new goog.events.EventHandler();
<a name="line222"></a>  var handlerObj = {
<a name="line223"></a>    handleEvent: goog.testing.recordFunction()
<a name="line224"></a>  };
<a name="line225"></a>  eh.listenOnce(target, &#39;click&#39;, handlerObj);
<a name="line226"></a>
<a name="line227"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line228"></a>  assertEquals(&#39;One event should have been dispatched&#39;,
<a name="line229"></a>      1, handlerObj.handleEvent.getCallCount());
<a name="line230"></a>
<a name="line231"></a>  target.dispatchEvent(&#39;click&#39;);
<a name="line232"></a>  assertEquals(&#39;No event should have been dispatched&#39;,
<a name="line233"></a>      1, handlerObj.handleEvent.getCallCount());
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>function testGetListenerCount() {
<a name="line237"></a>  eh = new goog.events.EventHandler();
<a name="line238"></a>  assertEquals(&#39;0 listeners registered initially&#39;, 0, eh.getListenerCount());
<a name="line239"></a>  var target = new goog.events.EventTarget();
<a name="line240"></a>  eh.listen(target, &#39;click&#39;, goog.nullFunction, false);
<a name="line241"></a>  eh.listen(target, &#39;click&#39;, goog.nullFunction, true);
<a name="line242"></a>  assertEquals(&#39;2 listeners registered&#39;, 2, eh.getListenerCount());
<a name="line243"></a>  eh.unlisten(target, &#39;click&#39;, goog.nullFunction, true);
<a name="line244"></a>  assertEquals(&#39;1 listener removed, 1 left&#39;, 1, eh.getListenerCount());
<a name="line245"></a>  eh.removeAll();
<a name="line246"></a>  assertEquals(&#39;all listeners removed&#39;, 0, eh.getListenerCount());
<a name="line247"></a>}
</pre>


</body>
</html>
